/*
-----------------------------------------------------------------------------
This source file is part of as4game
    (ActionScript For Game Engine)
For the latest info, see http://as4game.googlecode.com

Copyright (c) 2000-2011 Hproof Software Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
package org.as4game.common.math
{
	import flash.geom.Rectangle;
	
	/**
	 * IntRect
	 */
	public class IntRect
	{
		public var left:int;
		public var top:int;
		public var right:int;
		public var bottom:int;
		
		// inline
		public function get width():int { return right - left; };
		public function get height():int { return bottom - top; };
		public function get size():int { return (right-left) * (bottom - top); };
		
		public function get isEmpty():Boolean{ return (right<=left || bottom<=top); }
		public function get isVisible():Boolean{ return (right > left && bottom > top); }
		
		public function toString():String{
			return [left, top, right, bottom].join();
		}
		
		public function clone():IntRect{
			return new IntRect( left, top, right, bottom );
		}
		
		// ===========================================================================		
		
		/**
		 * 构造函数, 注意是 left, top, right, bottom 格式
		 */
		public function IntRect(l:int=0, t:int=0, r:int=0, b:int=0){
			left = l; top = t; right = r; bottom = b;
		}
		
		/**
		 * 从 x/y/width/height 方式构造
		 */
		static public function fromWH( x:int, y:int, width:int, height:int ):IntRect{
			return new IntRect(x, y, x+width, y+height);
		}
		
		/**
		 * 从 Rectangle 中构造
		 */
		static public function fromRect( rc:Rectangle ):IntRect{
			return new IntRect(rc.x, rc.y, rc.width, rc.height );
		}
		
		/**
		 * 设置 left/top/right/bottom
		 */
		public function setValue(l:int, t:int, r:int, b:int):void{
			left = l; top = t; right = r; bottom = b;
		}
		
		/**
		 * 设置为 Rectangle
		 */
		public function setRect( rc:Rectangle ):void{
			left = rc.left; top = rc.top; right = rc.right; bottom = rc.bottom;
		}
		
		/**
		 * 获取 Rectangle 格式
		 */
		public function toRect():Rectangle { 
			return new Rectangle(left, top, right-left, bottom-top); 
		}
		
		/**
		 * 设置为空矩形
		 */
		public function setEmpty():void{ 
			left = top = right = bottom = 0;
		}
		
		/**
		 * 设置为最大矩形
		 */
		public function setMax():void{
			left = top = int.MIN_VALUE / 2;
			right = bottom = int.MAX_VALUE / 2;
		}
		
		/**
		 * 必要时交换 left/right, top/bottom 使得矩形可见
		 */
		public function makeValid():void{
			var tmp:int;
			if( left > right ){
				tmp=left, left=right, right=tmp;
			} 
			if( top > bottom ){
				tmp=top, top=bottom, bottom=tmp;
			}
		}
		
		/**
		 * 移动偏移量
		 */
		public function offset( x:int, y:int ):void{
			left += x; right += x; top += y; bottom += y;
		}
		
		/**
		 * 移动到目标位置
		 */
		public function moveTo( x:int, y:int ):void{
			x = x - left;
			y = y - top;
			left += x; right += x; top += y; bottom += y;
		}
		
		/**
		 * 是否相等
		 * @param rc [const] 参考矩形
		 */
		public function isEqual( rc:IntRect ):Boolean{
			return (left == rc.left 
				&& top == rc.top 
				&& right == rc.right
				&& bottom == rc.bottom);
		}
		
		/**
		 * 取并集
		 * @param rc [const] 参考矩形
		 */
		public function or( rc:IntRect ):IntRect{
			if( rc.left < left ) left = rc.left;
			if( rc.top < top ) top = rc.top;
			if( rc.right > right ) right = rc.right;
			if( rc.bottom > bottom) bottom = rc.bottom;
			return this;
		}
		
		/**
		 * 交集
		 * @param rc [const] 参考矩形
		 */
		public function and( rc:IntRect ):IntRect{
			if( rc.left > left ) left = rc.left;
			if( rc.top > top ) top = rc.top;
			if( rc.right < right ) right = rc.right;
			if( rc.bottom < bottom) bottom = rc.bottom;
			return this;
		}
		
		/**
		 * 快速判断是否有交集
		 * @param rc [const] 参考矩形
		 */
		public function testAnd( rc:IntRect ):Boolean{
			return (	rc.right > left 	&&
						rc.left < right 	&&
						rc.top < bottom 	&&
						rc.bottom > top 	);
		}
		
		/**
		 * 获取 blt 操作时的源矩形范围
		 * <li> 把 src 中 clip(可选) 指定的矩形范围, 复制到 dst 中的 x/y 位置
		 * <li> clip 的左上角, 对齐到 dst 中的 x/y 位置
		 * @param dst [const] 源矩形
		 * @param x/y 源矩形中的 对齐点, clip 中的左上角, 将被对其到 dst 中的 x/y 位置, 然后计算传送范围
		 * @param src [const] 目标矩形
		 * @param clip [const] 目标矩形中的剪裁区域
		 * @return 返回在 src 矩形中, 可以被 blt 的实际矩形范围. 如果不可见, 返回 null
		 */
		static public function getBltRect(dst:IntRect, x:int, y:int, src:IntRect, clip:IntRect=null):IntRect{
			if( dst.isEmpty || src.isEmpty ) return null;
			
			// 取源范围剪裁
			var rc:IntRect = src.clone();
			if( clip && rc.and(clip).isEmpty ) return null;
			
			// 把 rc 移动到 dst 坐标空间
			var offx:int = x - rc.left;
			var offy:int = y - rc.top;
			rc.offset( offx, offy );
			
			// 和 dst 做剪裁
			if( rc.and( dst ).isEmpty ) return null;
			
			// 返回 源 空间坐标
			rc.offset( -offx, -offy );
			return rc;
		}

	}
}